package com.asl.website.feature.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.asl.website.feature.domain.TransportFeatureDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.asl.common.annotation.Log;
import com.asl.common.core.controller.BaseController;
import com.asl.common.core.domain.AjaxResult;
import com.asl.common.enums.BusinessType;
import com.asl.website.feature.domain.TTransportFeature;
import com.asl.website.feature.service.ITTransportFeatureService;
import com.asl.common.utils.poi.ExcelUtil;
import com.asl.common.core.page.TableDataInfo;

/**
 * 运输功能Controller
 * 
 * @author ruoyi
 * @date 2025-05-21
 */
@RestController
@RequestMapping("/feature/feature")
public class TTransportFeatureController extends BaseController
{
    @Autowired
    private ITTransportFeatureService tTransportFeatureService;

    @GetMapping("/list/{typeCode}")
    public AjaxResult getFeaturesByType(
            @PathVariable("typeCode") Long typeCode,
            @RequestHeader(value = "Accept-Language", defaultValue = "zh-CN") String acceptLanguage) {

        // 1. 构建查询条件
        TTransportFeature query = new TTransportFeature();
        query.setTypeCode(typeCode);

        // 2. 查询数据库
        List<TTransportFeature> features = tTransportFeatureService.selectTTransportFeatureList(query);

        // 3. 处理空数据情况
        if (features == null || features.isEmpty()) {
            return AjaxResult.error("未找到类型编码为[" + typeCode + "]的运输功能");
        }

        // 4. 转换为DTO并排序
        List<TransportFeatureDTO> result = features.stream()
                .map(feature -> new TransportFeatureDTO(feature, acceptLanguage))
                .sorted(Comparator.comparingInt(TransportFeatureDTO::getSortOrder))
                .collect(Collectors.toList());

        // 5. 返回成功结果
        return AjaxResult.success("查询成功", result);
    }

    /**
     * 查询运输功能列表
     */
    @PreAuthorize("@ss.hasPermi('feature:feature:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTransportFeature tTransportFeature)
    {
        startPage();
        List<TTransportFeature> list = tTransportFeatureService.selectTTransportFeatureList(tTransportFeature);
        return getDataTable(list);
    }

    /**
     * 导出运输功能列表
     */
    @PreAuthorize("@ss.hasPermi('feature:feature:export')")
    @Log(title = "运输功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTransportFeature tTransportFeature)
    {
        List<TTransportFeature> list = tTransportFeatureService.selectTTransportFeatureList(tTransportFeature);
        ExcelUtil<TTransportFeature> util = new ExcelUtil<TTransportFeature>(TTransportFeature.class);
        util.exportExcel(response, list, "运输功能数据");
    }

    /**
     * 获取运输功能详细信息
     */
    @PreAuthorize("@ss.hasPermi('feature:feature:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tTransportFeatureService.selectTTransportFeatureById(id));
    }

    /**
     * 新增运输功能
     */
    @PreAuthorize("@ss.hasPermi('feature:feature:add')")
    @Log(title = "运输功能", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTransportFeature tTransportFeature)
    {
        return toAjax(tTransportFeatureService.insertTTransportFeature(tTransportFeature));
    }

    /**
     * 修改运输功能
     */
    @PreAuthorize("@ss.hasPermi('feature:feature:edit')")
    @Log(title = "运输功能", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTransportFeature tTransportFeature)
    {
        return toAjax(tTransportFeatureService.updateTTransportFeature(tTransportFeature));
    }

    /**
     * 删除运输功能
     */
    @PreAuthorize("@ss.hasPermi('feature:feature:remove')")
    @Log(title = "运输功能", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tTransportFeatureService.deleteTTransportFeatureByIds(ids));
    }
}
